package com.atguigu.flink.watermark;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import com.atguigu.flink.utils.MyUtil;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2023/4/8
 */
public class Demo4_CustomWatermarkStrategy
{
    public static void main(String[] args) {
        
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);


        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            .<WaterSensor>forGenerator(new WatermarkGeneratorSupplier<WaterSensor>()
            {
                //返回一个WatermarkGenerator对象
                @Override
                public WatermarkGenerator<WaterSensor> createWatermarkGenerator(Context context) {
                    return new MyWatermarkGenerator(1000);
                }
            })
            .withTimestampAssigner((e, ts) -> e.getTs());

        env
                   .socketTextStream("hadoop102", 8888)
                   .map(new WaterSensorMapFunction())
                   .assignTimestampsAndWatermarks(watermarkStrategy)
                   .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                   .process(new ProcessAllWindowFunction<WaterSensor, String, TimeWindow>()
                   {
                       @Override
                       public void process(Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                           out.collect(context.window() + ":" + MyUtil.parseToList(elements));

                       }
                   })
                   .print();

        
                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
        
    }

    /*
        规则：
            ①保证生成的水印是单调递增
            ②发送水印，需要一个WatermarkOutput。
                    既可以在onEvent 发送水印， 断点式发送。
                    也可以在onPeriodicEmit 发送水印，周期性发送
            ③水印一定基于数据的时间属性制作而成
                    水印的 ts <= 数据的时间属性

                    自动推迟1s
     */
    private static class MyWatermarkGenerator implements WatermarkGenerator<WaterSensor>{

        private long maxTimestamp  ;
        private long late ;

        public MyWatermarkGenerator(long late){
            this.late = late;
            maxTimestamp = Long.MIN_VALUE + late + 1;
        }
        //来一条数据调用一次
        @Override
        public void onEvent(WaterSensor event, long eventTimestamp, WatermarkOutput output) {
            maxTimestamp =  Math.max(maxTimestamp,eventTimestamp);
          /*  Watermark watermark = new Watermark(maxTimestamp - late);
            System.out.println("向下游发送了:"+watermark.getTimestamp());
            output.emitWatermark(watermark);*/

        }

        //周期性发送水印
        @Override
        public void onPeriodicEmit(WatermarkOutput output) {
            Watermark watermark = new Watermark(maxTimestamp - late);
            System.out.println("向下游发送了:"+watermark.getTimestamp());
            output.emitWatermark(watermark);

        }
    }
}
